/*
	PlAr is Platform Arena: a 2D multiplayer shooting game
	Copyright (c) 2010, Antonio Ragagnin <spocchio@gmail.com>
    All rights reserved.

    This file is licensed under the New BSD License.
*/

package plar.core;

import java.util.ArrayList;

import org.jbox2d.dynamics.BodyType;
import org.jbox2d.dynamics.BodyDef;
import plar.core.Action;
import plar.core.Common;
import plar.core.Element;

/**
 * Bullet is a kind of element that toggle the amount of energy hitDamage when
 * collides with a player
 *
 * @author Antonio Ragagnin
 *
 */
public class ElementBullet extends Element {
    public ElementPlayer owner;
    public int hitDamage;
    public float speed;
    public float gap;
    CollisionPlayer at;
    public ElementBullet() {
        super();

        type = Common.ElementType.BULLET;

        // setType(BodyType.KINEMATIC);
        isRunnable = true;
        setStaticSprite("res/fire.png");
        hitDamage = 10;
        speed = 10;
        at = new CollisionPlayer(gap);
        at.setTimer(false);
        actions.addAction("collision", at);
        actions.addAction("nogravity", new ActionNoGravity());
        this.setBoxShape(0.3f, 0.3f);
    }
    public void setTimer(boolean t)
    {
        at.setTimer(t);
    }
    public void setBodyDef(BodyDef bd)
    {
        bd.type = BodyType.DYNAMIC;
    }
    class CollisionPlayer extends ActionTouch {
        public CollisionPlayer(float t) {
            super(t);
            repeat=true;
        }
        public boolean collision(Element me, Element e) {
            if (e.type == Common.ElementType.PLAYER)
            {
                ((ElementPlayer) e).energy -= hitDamage;
                ((ElementPlayer) e).lastTouch=owner;
            }
            return true;
        }

        public void touched() {

            me.actions.forceAction("destroy", null);
        }

    }
}
